<include file="public@header_vue"/>
<script src="https://cdn.jsdelivr.net/npm/vue-ueditor-wrap@latest/lib/vue-ueditor-wrap.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.all.min.js"></script>
</head>
<style>
    .el-pagination {
        margin-top: 20px;
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .photos {
        width: 120px;
        height: 120px;
        border-radius: 6px;
        overflow: hidden;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
    }

    .photos-options {
        position: absolute;
        top: 0;
        left: 0;
        height: 120px;
        line-height: 120px;
        width: 120px;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
        z-index: 10;
        opacity: 0;
        transition: opacity 0.6s;
    }

    .photos:hover .photos-options {
        opacity: 1;
    }

    .photos-options .el-button {
        font-size: 24px;
        color: white
    }

    .photos img {
        width: 120px;
        height: 120px;
    }

    .photos-uploader {
        width: 120px;
        height: 120px;
        float: left;
    }

    .logo-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .logo-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
</style>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="订单管理" v-loading="onDataLoading">
            <el-row>
                <el-col :span="24">
                    <el-form :inline="true" :model="form" size="mini">
                        <el-form-item label="日期:">
                            <el-date-picker v-model="form.duration" type="daterange" value-format="yyyy-MM-dd"
                                            :picker-options="durationOptions" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                        </el-form-item>
                        <el-form-item label="订单状态:">
                            <el-select v-model="form.order_status" placeholder="选择订单状态">
                                <el-option
                                        v-for="item in order_status"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                            <el-select v-model="form.pay_status" placeholder="选择支付状态">
                                <el-option
                                        v-for="item in pay_status"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                            <el-select v-model="form.shipping_status" placeholder="选择发货状态">
                                <el-option
                                        v-for="item in shipping_status"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="getData()">查询</el-button>
                            <el-button type="danger" @click="clearForm()">清空</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="order_sn" label="订单号" min-width="240"></el-table-column>
                <el-table-column prop="user_name" label="用户昵称"></el-table-column>
                <el-table-column v-if="{$role_id} != 3" prop="company_name" label="公司名称"></el-table-column>
                <el-table-column prop="order_status" label="订单状态" min-width="200">
                    <template slot-scope="scope">
                        {{scope.row.order_status}}-{{scope.row.pay_status}}-{{scope.row.shipping_status}}
                    </template>
                </el-table-column>
                <el-table-column prop="order_price" label="订单价格"></el-table-column>
                <el-table-column prop="num" label="商品总数"></el-table-column>
                <el-table-column prop="real_price" label="实际价格"></el-table-column>
                <el-table-column prop="pay_points" label="支付积分"></el-table-column>
                <el-table-column prop="gift_points" label="赠送积分"></el-table-column>
                <el-table-column prop="remark" label="订单备注" width="200"></el-table-column>
                <el-table-column prop="consignee" label="收货人" min-width="100"></el-table-column>
                <el-table-column prop="mobile" label="联系方式" min-width="150"></el-table-column>
                <el-table-column prop="address" label="收货地址" min-width="200"></el-table-column>
                <el-table-column prop="create_time" label="操作时间" min-width="200">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="update(scope.row)">查看</el-button>
                        <el-button size="mini"
                                   v-if="scope.row.order_status == '确认订单' && scope.row.pay_status == '已支付' &&scope.row.shipping_status == '未发货'"
                                   type="text" @click="express(scope.row.order_sn)">发货
                        </el-button>
                        <el-button size="mini" v-if="scope.row.pay_status == '已支付'" type="text"
                                   @click="refund(scope.row.order_sn,scope.row.money_paid)">退款
                        </el-button>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="totalNum" :page-size="10"
                           :current-page.sync="currentPage" @current-change="getData"></el-pagination>
        </el-tab-pane>
    </el-tabs>
    <el-dialog title="快递信息" :visible.sync="expressDialogVisible" width="60%"
               v-loading.fullscreen="dialogLoading">
        <el-form :model="expressForm" class="demo-form-inline" :rules="rules" ref="expressForm"
                 label-width="200px">
            <el-form-item label="快递公司" prop="express_name">
                <el-col :span="18">
                    <el-input v-model="expressForm.express_name"
                              placeholder="请输入快递公司名称"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="快递单号" prop="express_number">
                <el-col :span="18">
                    <el-input v-model="expressForm.express_number" placeholder="请输入快递单号"></el-input>
                </el-col>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submit('expressForm')">发货</el-button>
        </span>
    </el-dialog>
    <el-dialog title="退款信息" :visible.sync="refundDialogVisible" width="60%"
               v-loading.fullscreen="dialogLoading">
        <el-form :model="refundForm" class="demo-form-inline" :rules="refundRules" ref="refundForm"
                 label-width="200px">
            <el-form-item label="退款金额" prop="refund_money">
                <el-col :span="18">
                    <el-input v-model="refundForm.refund_money" type="number" :max="refundForm.max_money"
                              :placeholder="'请输入退款金额,最大'+ refundForm.max_money"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="退款原因" prop="refund_reason">
                <el-col :span="18">
                    <el-input type="textarea" v-model="refundForm.refund_reason" placeholder="请输入退款原因"></el-input>
                </el-col>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submit('refundForm')">退款</el-button>
        </span>
    </el-dialog>
    <el-dialog :visible.sync="orderDetailVisible" width="60%" title="订单信息">
        <el-tabs>
            <el-tab-pane label="订单信息">
                <el-form :model="orderDetail" label-width="100px">
                    <el-form-item label="订单号:">
                        {{orderDetail.order_sn}}
                    </el-form-item>
                    <el-form-item label="用户昵称:">
                        {{orderDetail.user_name}}
                    </el-form-item>
                    <el-form-item label="手机号码:">

                    </el-form-item>
                    <el-form-item label="支付单号:">
                        {{orderDetail.pay_sn}}
                    </el-form-item>
                    <el-form-item label="下单时间:">
                        {{formatDate(orderDetail.create_time)}}
                    </el-form-item>
                    <el-form-item label="支付时间:">
                        {{formatDate(orderDetail.pay_time)}}
                    </el-form-item>
                    <el-form-item label="订单状态:">
                        {{orderDetail.order_status}}-{{orderDetail.pay_status}}-{{orderDetail.shipping_status}}
                    </el-form-item>
                    <el-form-item label="订单金额">
                        {{orderDetail.order_price}}
                    </el-form-item>
                    <el-form-item label="优惠金额">
                        {{orderDetail.coupon_money}}
                    </el-form-item>
                    <el-form-item label="支付积分">
                        {{orderDetail.pay_points}}
                    </el-form-item>
                    <el-form-item label="实付金额">
                        {{orderDetail.real_price}}
                        <el-button v-if="orderDetail.pay_status == '待支付' && orderDetail.order_status != '取消订单'"
                                   @click="change_price(orderDetail.order_sn)">修改订单价格
                        </el-button>
                    </el-form-item>
                    <el-form-item label="支付金额">
                        {{orderDetail.money_paid}}
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane label="商品信息">
                <el-table :data="orderDetail.order_goods_list">
                    <el-table-column prop="id" label="ID" width="80"></el-table-column>
                    <el-table-column prop="goods_name" label="商品名称" width="80"></el-table-column>
                    <el-table-column prop="goods_price" label="单价" width="80"></el-table-column>
                    <el-table-column prop="num" label="个数" width="80"></el-table-column>
                    <el-table-column prop="total_price" label="总价" width="80"></el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane v-if="orderDetail.coupon_info" label="优惠券信息">
                <el-form :model="orderDetail" label-width="100px">
                    <el-form-item label="优惠券名称:">
                        {{orderDetail.coupon_info.title}}
                    </el-form-item>
                    <el-form-item v-if="orderDetail.coupon_info.coupon_type == 'DISCOUNT'" label="折扣:">
                        {{(100-orderDetail.coupon_info.discount)/10}}折
                    </el-form-item>
                    <el-form-item label="卡券card_id:">
                        {{orderDetail.card_id}}
                    </el-form-item>
                    <el-form-item label="card_code:">
                        {{orderDetail.card_code}}
                    </el-form-item>
                    <el-form-item label="卡券状态:">
                        {{orderDetail.coupon_status}}
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane v-if="orderDetail.refund_info" label="申请退款信息">
                <el-form :model="orderDetail.refund_info" label-width="100px">
                    <el-form-item label="申请时间:">
                        {{formatDate(orderDetail.refund_info.create_time)}}
                    </el-form-item>
                    <el-form-item label="申请金额:">
                        {{orderDetail.refund_info.money}}
                    </el-form-item>
                    <el-form-item label="理由:">
                        {{orderDetail.refund_info.reason}}
                    </el-form-item>
                    <el-form-item label="理由:">
                        {{orderDetail.refund_info.status}}
                    </el-form-item>
                </el-form>
                <el-button v-if="orderDetail.refund_info.status == '待处理'" type="primary"
                           @click="change_refund_status(orderDetail.order_sn,1)">同意
                </el-button>
                <el-button v-if="orderDetail.refund_info.status == '待处理'" type="danger"
                           @click="change_refund_status(orderDetail.order_sn,2)">拒绝
                </el-button>
            </el-tab-pane>
            <el-tab-pane v-if="orderDetail.pay_status=='已退款'" label="退款信息">
                <el-form :model="orderDetail.refund_info" label-width="100px">
                    <el-form-item label="退款时间:">
                        {{formatDate(orderDetail.refund_time)}}
                    </el-form-item>
                    <el-form-item label="退款金额:">
                        {{orderDetail.refund_money}}
                    </el-form-item>
                    <el-form-item label="退款单号:">
                        {{orderDetail.refund_order_sn}}
                    </el-form-item>
                    <el-form-item label="退款原因:">
                        {{orderDetail.refund_reason}}
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane v-if="orderDetail.shipping_status == '已发货' || orderDetail.shipping_status == '已收货'"
                         label="快递信息">
                <el-form :model="orderDetail.refund_info" label-width="100px">
                    <el-form-item label="发货时间:">
                        {{formatDate(orderDetail.shipping_time)}}
                    </el-form-item>
                    <el-form-item label="快递公司:">
                        {{orderDetail.express_name}}
                    </el-form-item>
                    <el-form-item label="快递号:">
                        {{orderDetail.express_number}}
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane v-if="orderDetail.admin_log_count > 0" label="管理员操作">
                <el-table :data="orderDetail.admin_log">
                    <el-table-column prop="id" label="ID" width="80"></el-table-column>
                    <el-table-column prop="admin_name" label="管理员" width="80"></el-table-column>
                    <el-table-column prop="remark" label="操作说明"></el-table-column>
                </el-table>
            </el-tab-pane>
        </el-tabs>
    </el-dialog>
</div>
</body>
<script>
    Vue.component('vue-ueditor-wrap', VueUeditorWrap)
    var app = new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                companyData: [],
                form: {
                    name: '',
                    order_status: '',
                    duration: '',
                },
                expressDialogVisible: false,
                refundDialogVisible: false,
                expressForm: {
                    order_sn: '',
                    express_number: '',
                    express_name: '',
                },
                order_status: [
                    {id: 0, name: '确认订单'},
                    {id: 1, name: '取消订单'},
                    {id: 2, name: '退款订单'},
                ],
                pay_status: [
                    {id: 0, name: '未支付'},
                    {id: 1, name: '待支付'},
                    {id: 2, name: '已支付'},
                    {id: 3, name: '已退款'},
                ],
                shipping_status: [
                    {id: 0, name: '未发货'},
                    {id: 1, name: '已发货'},
                    {id: 2, name: '已退货'},
                    {id: 3, name: '已收货'},
                ],
                refundForm: {
                    order_sn: '',
                    refund_money: '',
                    max_money: '',
                    refund_reason: ''
                },
                orderDetail: [],
                myConfig: {
                    // 编辑器不自动被内容撑高
                    autoHeightEnabled: false,
                    // 初始容器高度
                    initialFrameHeight: 400,
                    zIndex: 3000,
                    // 初始容器宽度
                    initialFrameWidth: '90%',
                    // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                    serverUrl: 'http://ml.0513app.cn/static/UEditor/php/controller.php',
                    // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
                    UEDITOR_HOME_URL: '/static/UEditor/',
                },
                totalNum: 0,
                currentPage: 1,
                dialogLoading: false,
                onDataLoading: false,
                orderDetailVisible: false,
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                sourceFrom: {
                    id: '',
                    name: '',
                    money: '',
                    activity_time: '',
                    sign_time: '',
                    give_points: '',
                    imgs: [],
                },
                dialogVisible: false,
                rules: {
                    express_name: [{required: true, message: '请输入快递公司名称', trigger: 'blur'}],
                    express_number: [{required: true, message: '请选输入快递单号', trigger: 'blur'}],
                },
                refundRules: {
                    refund_money: [{required: true, message: '请输入退款金额', trigger: 'blur'}],
                    refund_reason: [{required: true, message: '请选输入退款原因', trigger: 'blur'}],
                },
                headers: {'X-Requested-With': 'XMLHttpRequest'},
            }
        },
        created() {
            this.getCompanyData();
            this.getData()
        },
        methods: {
            clearForm() {
                this.form = {
                    name: '',
                    order_status: '',
                    duration: '',
                    pay_status: '',
                    shipping_status: '',
                }
            },
            change_refund_status(order_sn, status) {
                let _this = this;
                let params = {order_sn: order_sn, status: status};
                axios.post("{:url('admin/order/change_refund_status')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            express(order_sn) {
                this.expressDialogVisible = true;
                this.dialogLoading = false;
                this.expressForm.order_sn = order_sn

            },
            change_price(order_sn) {
                console.log(order_sn)
                let _this = this;
                this.dialogLoading = true;

                this.$prompt('请输入修改后价格', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /^[1-9]\d*(\.\d{1,2})?$|^0+(\.\d{1,2})?$/,
                    inputErrorMessage: '请输入正确的价格'
                }).then(({value}) => {
                    let params = {order_sn: order_sn, price: value};
                    axios.post("{:url('admin/order/change_price')}", params, {
                        headers: {
                            'X-Requested-With': 'XMLHttpRequest',
                        }
                    }).then(function (response) {
                        _this.dialogLoading = false;
                        if (response.data.code === 1) {
                            _this.$message.success('修改完成');
                            _this.getData(_this.currentPage);
                        } else {
                            _this.$message.warning(response.data.msg);
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });
                });


            },
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                if (this.form.order_status !== '') {
                    params.order_status = this.form.order_status;
                }
                if (this.form.pay_status !== '') {
                    params.pay_status = this.form.pay_status;
                }
                if (this.form.shipping_status !== '') {
                    params.shipping_status = this.form.shipping_status;
                }
                axios.get("{:url('admin/order/index')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.data;
                        _this.totalNum = response.data.data.total;
                        console.log(_this.totalNum)
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            update(data) {
                let _this = this;
                if (data) {
                    _this.orderDetail = JSON.parse(JSON.stringify(data));
                } else {
                    _this.sourceFrom = {
                        id: '',
                        title: '',
                        money: 0,
                        activity_time: '',
                        sign_time: '',
                        give_points: 0,
                        imgs: [],
                        cover_img: '',
                        quota_num: 0,
                    }
                }
                _this.orderDetailVisible = true;
                _this.dialogLoading = false;
            },
            refund(order_sn, money_paid) {
                this.refundDialogVisible = true;
                this.refundForm = {
                    order_sn: order_sn,
                    max_money: money_paid,
                    refund_money: '',
                    refund_reason: '',
                }
            },
            submit(form) {
                let _this = this;
                switch (form) {
                    case 'expressForm':
                        _this.$refs[form].validate((valid) => {
                            if (valid) {
                                axios.post("{:url('admin/order/express')}", _this.expressForm, {
                                    headers: {
                                        'X-Requested-With': 'XMLHttpRequest',
                                    }
                                }).then(function (response) {
                                    _this.dialogLoading = false;
                                    if (response.data.code === 1) {

                                        _this.$message.success(response.data.msg);
                                        _this.getData(_this.currentPage);
                                        _this.expressDialogVisible = false;
                                    } else {
                                        _this.$message.warning(response.data.msg);
                                    }
                                }).catch(function (error) {
                                    _this.dialogLoading = false;
                                    _this.$message.error(error.toString())
                                })
                            }
                        })
                        break;
                    case 'refundForm':
                        _this.$refs[form].validate((valid) => {
                            if (valid) {
                                axios.post("{:url('admin/order/refund')}", _this.refundForm, {
                                    headers: {
                                        'X-Requested-With': 'XMLHttpRequest',
                                    }
                                }).then(function (response) {
                                    _this.dialogLoading = false;
                                    if (response.data.code === 1) {

                                        _this.$message.success(response.data.msg);
                                        _this.getData(_this.currentPage);
                                        _this.refundDialogVisible = false;
                                    } else {
                                        _this.$message.warning(response.data.msg);
                                    }
                                }).catch(function (error) {
                                    _this.dialogLoading = false;
                                    _this.$message.error(error.toString())
                                })
                            }
                        })
                        break;
                }

            },
            formatDate(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp * 1000);
                    return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
                } else {
                    return '';
                }
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
